/*
 * Copyright (c) 2011 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.snj.visangedu.calendar;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkUtil {

  public static int TYPE_WIFI = 1;
  public static int TYPE_MOBILE = 2;
  public static int TYPE_NOT_CONNECTED = 0;

  public static int iStatus;

  public static int getConnectivityStatus(Context context) {
    ConnectivityManager cm =
        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

    if (null != activeNetwork) {
      if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
        NetworkUtil.iStatus = NetworkUtil.TYPE_WIFI;
        return NetworkUtil.TYPE_WIFI;
      }

      if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
        NetworkUtil.iStatus = NetworkUtil.TYPE_MOBILE;
        return NetworkUtil.TYPE_MOBILE;
      }
    }
    NetworkUtil.iStatus = NetworkUtil.TYPE_NOT_CONNECTED;
    return NetworkUtil.TYPE_NOT_CONNECTED;
  }

  public static String getConnectivityStatusString(Context context) {
    int conn = NetworkUtil.getConnectivityStatus(context);
    String status = null;
    if (conn == NetworkUtil.TYPE_WIFI) {
      status = "Wifi enabled";
    } else if (conn == NetworkUtil.TYPE_MOBILE) {
      status = "Mobile data enabled";
    } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
      status = "Not connected to Internet";
    }
    return status;
  }
}
